<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('会员列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="case-form">
                <div class="select-list">
                    <ul>
                        <li>
                            <p style="width: 90px;">微信昵称：</p>
                            <input type="text" name="nickname"/>
                        </li>
                        <li>
                            <p style="width: 90px;">会员名：</p>
                            <input type="text" name="userName"/>
                        </li>
                        <li>
                            <p style="width: 90px;">会员编号：</p>
                            <input type="text" name="memberSn"/>
                        </li>
                        <li>
                            <p style="width: 90px;">手机号：</p>
                            <input type="text" name="phone"/>
                        </li>
                        <li>
                            <p style="width: 90px;">邮箱：</p>
                            <input type="text" name="email"/>
                        </li>
                        <li>
                            <p style="width: 90px;">是否VIP：</p>
                            <select name="isVip" th:with="type=${@dict.getType('is_enable')}">
                            <option value="-1">全部</option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                    th:value="${dict.dictValue}"></option>
                        </select>
                        </li>
                        <li class="select-time">
                            <p style="width: 90px;">注册时间：</p>
                            <input type="text" class="time-input" id="startTime" placeholder="开始时间"
                                   name="params[beginTime]"/>
                            <span>-</span>
                            <input type="text" class="time-input" id="endTime" placeholder="结束时间"
                                   name="params[endTime]"/>
                        </li>
                        <li>
                            <p style="width: 90px;">所属店铺：</p>
                            <select name="shopId" th:with="shopList=${shopList}">
                                <option value="">全部</option>
                                <option th:each="shop:${shopList}" th:value="${shop.id}"
                                        th:text="${shop.shopName}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-warning multiple disabled" onclick="exportSelect()"
               shiro:hasPermission="back:member:export">
                <i class="fa fa-download"></i> 选择导出
            </a>
            <a class="btn btn-primary multiple disabled" onclick="multiEdit(1)">
                <i class="fa fa-check"></i> 批量设置VIP
            </a>
            <a class="btn btn-danger multiple disabled" onclick="multiEdit(0)">
                <i class="fa fa-remove"></i> 批量取消VIP
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var prefix = ctx + "back/member";
    const editFlag = [[${@permission.hasPermi('back:member:edit')}]];
    const removeFlag = [[${@permission.hasPermi('back:member:remove')}]];

    const yesNoDict = [[${@dict.getType('is_enable')}]]

    function multiEdit(val) {
        var rows = $.table.selectFirstColumns();
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        let updateData = {
            ids: rows.join(),
            isVip: val
        };
        $.ajax({
            type: 'post',
            url: ctx + "back/member/edit",
            data: updateData,
            dataType: 'json',
            async: false,
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess("操作成功！");
                } else {
                    $.modal.alertError(result.msg);
                }
                $.table.refresh();
            }
        });
    }

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            height:600,
            modalName: "会员",
            sortName: "createTime",
            sortOrder: "desc",
            columns: [
                {
                    checkbox:true
                },
                {
                    field: 'id',
                    title: "ID",
                    sortable: true
                },
                {
                    field: 'shopName',
                    title: '所属店铺',
                    sortable: true
                },
                {
                    field: 'memberSn',
                    title: "会员编号",
                    sortable: true
                },
                {
                    field: 'nickname',
                    title: '微信昵称'
                },
                {
                    field: 'userName',
                    title: "会员名",
                },
                {
                    field: 'avatar',
                    title: '头像',
                    formatter: function (value, row, index) {
                        return $.common.sprintf("<img class='img-circle img-sm' data-height='%s' data-width='%s' data-target='%s'  style='border-radius: 0;' src='%s'/>", 500, 'auto', 'self', value);
                    }
                },

                {
                    field: 'isVip',
                    title: '是否VIP',
                    sortable: true,
                    formatter: function (value, item, index) {
                        return $.table.selectDictLabel(yesNoDict, value);
                    }
                },
                {
                    field: 'phone',
                    title: '手机号',
                },
                {
                    field: 'email',
                    title: '邮箱',
                },
                {
                    field: 'credit',
                    title: '积分',
                    sortable: true
                },
                {
                    field: 'loginTime',
                    title: '上次登录时间',
                    sortable: true
                },
                {
                    field: 'createTime',
                    title: '注册时间',
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit">编辑</i></a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-trash"></i>删除</a>');
                        return actions.join('');
                    }
                }
            ]
        };
        $.table.init(options);
    });

    //选择数据导出
    function exportSelect() {
        var rows = $.table.selectFirstColumns();
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        $.modal.confirm("确认要导出的" + rows.length + "条数据吗？", function () {
            var data = {};
            if (rows.length === 1) {
                data.memberId = rows[0];
            } else {
                data.memberIdStr = rows.join()
            }
            $.modal.loading("正在导出数据，请稍后...");
            $.post(prefix + "/export", data, function (result) {
                if (result.code == web_status.SUCCESS) {
                    window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                } else if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                } else {
                    $.modal.alertError(result.msg);
                }
                $.modal.closeLoading();
            });
        });
    }
</script>
</body>
</html>